<?php
namespace app\admin\controller;

use app\AdminCollection;
use app\model\Menu;
use My\Tool;
use My\Validate;
use think\Db;
use think\Cache;

class Auth extends AdminCollection
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 菜单列表
     * @return mixed
     */
    public function index()
    {
        $_list = [];
        $list = Menu::menuTree();
        $keyorder = 0;
        foreach ($list as $key => $val) {
            $_t = $val;
            $_t['keyorder'] = $keyorder += 1;
            $_t['statusString'] = $this->getConfig('status', $val['status']);
            $_t['displayString'] = $this->getConfig('is_display', $val['is_display']);
            $_t['inputtime'] = date('Y-m-d H:i:s', $val['inputtime']);
            $_t['updateurl'] = url('auth/update', array('menuid' => $val['menu_id']));
            $_t['deleteurl'] = url('auth/delete', array('menuid' => $val['menu_id']));
            $_list[] = $_t;
        }
        $this->assign('list', $_list);
        return $this->fetch();
    }

    /**
     * 排序菜单
     */
    public function listorder()
    {
        $input = input('');
        $data = $input['list_order'];
        $where = $input['where'];
        foreach ($where as $key => $val) {
            Menu::updateServer(['menu_id' => $val], ['list_order' => $data[$key]]);
        }
        $this->returnSuccess("提交数据成功");
    }

    /**
     * 删除菜单
     */
    public function delete()
    {
        Db::startTrans();
        try {
            $menuid = input('menuid');
            if (Validate::checkNull($menuid)) throw new \Exception("删除失败");
            $findService = Menu::findService(['menu_id' => $menuid]);
            if (Validate::checkEmpty($findService)) throw new \Exception("删除失败");
            $parentFind = Menu::findService(['pid' => $menuid]);
            if ($parentFind) throw new \Exception("请先删除子级");
            $return = Menu::deleteService(['menu_id' => $menuid]);
            if ($return) {
                Cache::set('cachemenu', null);
                Db::commit();
                $this->returnSuccess("删除数据成功");
            } else {
                throw new \Exception("删除数据失败");
            }
        } catch (\Exception $e) {
            Db::rollback();
            $this->returnError($e->getMessage());
        }
    }

    /**
     * 修改菜单
     * @return mixed
     */
    public function update()
    {
        $submit = input('submit');
        if ($submit) {
            try {
                $input = input('');
                $data = $input['info'];
                $where = $input['where'];
                if (Validate::checkNull($data['title'])) throw new \Exception("请输入菜单标题");
                if (Validate::checkNull($data['name'])) throw new \Exception("请输入操作方法");
                if (Validate::checkMenuName($data['name'])) throw new \Exception("请重新输入操作方法");
                $data['name'] = strtolower($data['name']);
                Menu::updateServer($where, $data);
                Cache::set('cachemenu', null);
                $this->returnSuccess("提交数据成功", $this->getPrevious());
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        } else {
            $menuid = input('menuid');
            if (Validate::checkNull($menuid)) $this->returnError("操作失败");
            $findService = Menu::findService(['menu_id' => $menuid]);
            if (Validate::checkEmpty($findService)) $this->returnError("操作失败");
            $this->assign('findService', $findService);

            $list = Menu::menuTree();
            $this->assign('list', $list);
            $this->assign('REFERER', $this->previousUrl());
            return $this->fetch();
        }
    }

    /**
     * 添加菜单
     * @return mixed
     */
    public function add()
    {
        $submit = input('submit');
        if ($submit) {
            try {
                $input = input('');
                $data = $input['info'];
                if (Validate::checkNull($data['title'])) throw new \Exception("请输入菜单标题");
                if (Validate::checkNull($data['name'])) throw new \Exception("请输入操作方法");
                if (Validate::checkMenuName($data['name'])) throw new \Exception("请重新输入操作方法");
                $data['name'] = strtolower($data['name']);
                $data['inputtime'] = time();
                $return = Menu::addService($data);
                if ($return) {
                    Cache::set('cachemenu', null);
                    $this->returnSuccess("提交数据成功");
                } else {
                    throw new \Exception("提交数据失败");
                }
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        } else {
            $list = Menu::menuTree();
            $this->assign('list', $list);
            return $this->fetch();
        }
    }
}
